package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.heima.behavior.service.CommentService;
import com.heima.common.util.RequestContextUtil;
import com.heima.feign.clients.ArticleClient;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.behavior.dtos.CommentDto;
import com.heima.model.behavior.dtos.CommentLikeDto;
import com.heima.model.behavior.dtos.CommentSearchDto;
import com.heima.model.behavior.pojos.CommentDoc;
import com.heima.model.behavior.pojos.CommentLikeDoc;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    ArticleClient articleClient;

    /**
     * 保存评论
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveComment(CommentDto dto) {
        ApArticle article = articleClient.findById(dto.getArticleId());
        Integer userId= RequestContextUtil.get("id");
        String userName= RequestContextUtil.get("name");
        String image=RequestContextUtil.get("image");
        CommentDoc commentDoc = new CommentDoc();
        commentDoc.setId(IdWorker.getId());
        commentDoc.setAuthorId(article.getAuthorId());
        commentDoc.setAuthorName(article.getAuthorName());
        commentDoc.setEntryId(article.getId());
        commentDoc.setChannelId(article.getChannelId());
        commentDoc.setType(0);
        commentDoc.setContent(dto.getContent());
        commentDoc.setImage(image);
        commentDoc.setLikes(0);
        commentDoc.setReply(0);
        commentDoc.setFlag(0);
        commentDoc.setCreatedTime(new Date());
        commentDoc.setUpdatedTime(new Date());
        commentDoc.setOrd(1);
        mongoTemplate.save(commentDoc);
        System.out.println("保存评论成功");
        return ResponseResult.okResult();
    }

    /**
     * 评论列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadComment(CommentSearchDto dto) {

        Query query = Query.query(Criteria.where("createdTime").lt(dto.getMinDate())
        .and("entryId").is(dto.getArticleId()));
        query.with(Sort.by(Sort.Direction.DESC,"createdTime"));
        query.limit(5);
        List<CommentDoc> list = mongoTemplate.find(query,CommentDoc.class);
        // 当前用户，评论点赞状态
        Integer userId=RequestContextUtil.get("id");
        for (CommentDoc commentDoc1 : list) {//遍历评论列表
            Query query1 = Query.query(Criteria.where("commentId").is(commentDoc1.getId())
            .and("userId").is(userId));
            List<CommentLikeDoc> likeDocs = mongoTemplate.find(query1, CommentLikeDoc.class);//哪些评论被点赞
            System.out.println(likeDocs.size());
            if (likeDocs.size()>0) {
                for (CommentLikeDoc likeDoc : likeDocs) {
                    if (commentDoc1.getId().longValue()==likeDoc.getCommentId().longValue()) {
                        commentDoc1.setOperation(0);
                    }
                }
            }
        }
        return ResponseResult.okResult(list);
    }

    /**
     * 点赞评论
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likeComment(CommentLikeDto dto) {
        Integer userId = RequestContextUtil.get("id");
        Query query = Query.query(Criteria.where("commentId").is(dto.getCommentId())
                .and("userId").is(userId));
        List<CommentLikeDoc> likeDocs = mongoTemplate.find(query, CommentLikeDoc.class);
        if (likeDocs.size() <= 0) {
            mongoTemplate.save(new CommentLikeDoc(IdWorker.getId(),userId,dto.getCommentId()));
            System.out.println("点赞评论成功");
            long count = mongoTemplate.count(query, CommentLikeDoc.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("likes",count);
            return ResponseResult.okResult(map);
        } else {
            mongoTemplate.remove(query, CommentLikeDoc.class);
            System.out.println("取消评论点赞");
            long count = mongoTemplate.count(query, CommentLikeDoc.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("likes",count);
            return ResponseResult.okResult(map);
        }
    }

}
